import { onMounted, ref, onUnmounted } from 'vue';
import { verifyPassword, verifyWithdraw } from "@/utils/validateTools";
import { MessageEvent2 } from '@/net/MessageEvent2';
import { NetMsgType } from '@/netBase/NetMsgType';
import pinia from '@/store/index';
import { storeToRefs } from 'pinia';
import { User } from '@/store/user';
import router from '@/router/index';
import { i18n } from "@/languages/index.ts";
import { NetPacket } from '@/netBase/NetPacket';
import H5Websocket from "@/net/Net1";
import {updateStorePayPwd} from "@/utils/useStoreMethods.ts";
const t = i18n.global.t;
const userInfo = User(pinia);
const { info, roleInfo } = storeToRefs(userInfo);
export const securityshowToast = ref(false)
export const toast_value = ref('')
const isModify = ref(false)   // 是否修改
const verifyTrue = ref(false)   // 验证通过
const useHooks = () => {
  const routineRef: any = ref(null)

  const routineFormData:any = ref({
    old_password: '',
    new_password: '',
    new_password_confirm:'',
  })
  const emailFormData = ref({
    old_password: '',
    new_password: "",
    new_password_confirm: "",
    codeValue: ''
  })
  //   const formRef = ref(null)
  const paramsData: any = ref({}); // 记录提交参数

  const passwordValidator = (val: any) => {
    if (!val) {
      return Promise.reject(t('home_page_enterPassword'))
    } else if (!verifyPassword(val)) {
      return Promise.reject(t('home_page_passwordFormatIncorrect'))
    } else {
      return Promise.resolve()
    }
  }
  // 资金密码验证
  const withdrawpasswordValidator = (val: any) => {
    if (!val) {
      return Promise.reject(t('home_page_enterPassword'))
    } else if (!verifyWithdraw(val)) {
      return Promise.reject('密码需由6-12位纯数字组成')
    } else {
      return Promise.resolve()
    }
  }

  const notify_result = (rs: any) => {
    if (rs.code == 1) {
      if (isModify.value) {
        isModify.value = false
        verifyTrue.value = true
      } else {
        securityshowToast.value = true
        toast_value.value = '操作成功'
        setTimeout(() => {
          router.push('/me')
        }, 2000);
      }
    }
  }

  // 资金密码修改回执
  const notify_withdraw_result = (rs: any) => {
    if (rs.code == 1) {
      updateStorePayPwd(paramsData.value.new_password);

      if (isModify.value) {
        isModify.value = false
        verifyTrue.value = true
      } else {
        securityshowToast.value = true
        toast_value.value = '操作成功';
        setTimeout(() => {
          router.push('/securitySettings')
        }, 2000);
      }
    }
  }

  // 更改密码或验证验证码
  // operate_type: 2, //1表示点击下一步需要校验手机或者邮箱是否已经绑定，验证码是否正确；operate_type=2表示立即修改密码
  // modify_type: 1, //1表示常规修改密码，modify_type=2表示通过手机修改密码，modify_type=3表示通过邮箱修改密码
  const changePassword = (params: any, type: number, name: string) => {

    let req = NetPacket.req_reset_account_password();
    // 点击下一步验证手机或邮箱是否绑定
    if (type == 1) {
      isModify.value = true
      req.operate_type = 1
    } else {
      isModify.value = false
      req.operate_type = 2
    }
    if (name == 'mobile') {
      req.username = params.codeValue + params.mobile
      req.captcha = params.verify_code
    }
    if (name == 'email') {
      req.username = params.email
      req.captcha = params.verify_code
    }
    req.modify_type = params.modify_type
    // req.username = info.value?.full_name
    req.old_password = params?.old_password
    req.new_password = params?.new_password
    req.new_password_confirm = params?.new_password_confirm
    H5Websocket.sendMessageHandler(req);
  };


  // 更改资金密码或验证验证码
  //operate_type=1表示绑定资金密码；operate_type=2表示常规修改资金密码；operate_type=3表示通过手机修改密码；operate_type=4表示通过邮箱修改密码
  const changewithdrawPassword = (params: any, type: number, name: string) => {
    paramsData.value = {...params}
    let req = NetPacket.req_bind_or_modify_withdraw_password();
    // 点击下一步验证手机或邮箱是否绑定
    if (type == 1) {
      isModify.value = true
    } else {
      isModify.value = false
    }
    if (name == 'mobile') {
      req.mobile_or_email = params.codeValue + params.mobile
      req.captcha = params.verify_code
    }
    if (name == 'email') {
      req.mobile_or_email = params.email
      req.captcha = params.verify_code
    }
    req.operate_type = params.operate_type
    // req.username = info.value?.full_name
    req.old_password = params?.old_password
    req.new_password = params?.new_password
    req.new_password_confirm = params?.new_password_confirm
    req.role_id = roleInfo.value?.id
    req.step = params.step
    H5Websocket.sendMessageHandler(req);
  };


  onMounted(() => {
    MessageEvent2.addMsgEvent(
      NetMsgType.msgType.msg_notify_reset_account_password,
      notify_result
    );
    MessageEvent2.addMsgEvent(  // 修改资金密码回执
      NetMsgType.msgType.msg_notify_bind_or_modify_withdraw_password,
      notify_withdraw_result
    );
  })
  onUnmounted(() => {
    securityshowToast.value = false
    // 取消监听
    MessageEvent2.removeMsgEvent(NetMsgType.msgType.msg_notify_reset_account_password, null);
    MessageEvent2.removeMsgEvent(NetMsgType.msgType.msg_notify_bind_or_modify_withdraw_password, null);

  });

  return {
    routineFormData,
    emailFormData,
    info,
    roleInfo,
    routineRef,
    passwordValidator,
    withdrawpasswordValidator,
    changePassword,
    verifyTrue,
    isModify,
    changewithdrawPassword
  }

}

export default useHooks;